import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:my_flutter_github/MyStyle.dart';
import 'package:my_flutter_github/common/dao/IssueDao.dart';
import 'package:my_flutter_github/utils/CommonUtils.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/widgets/BaseListState.dart';
import 'package:my_flutter_github/widgets/IssueItemWidget.dart';
import 'package:my_flutter_github/widgets/PullLoadWidget.dart';
import 'package:my_flutter_github/widgets/SearchInputWidget.dart';
import 'package:my_flutter_github/widgets/SelectItemWidget.dart';

/// 仓库详情issue列表
class RepositoryDetailIssuePage extends StatefulWidget {
  final String userName;

  final String reposName;

  RepositoryDetailIssuePage(this.userName, this.reposName);

  @override
  _RepositoryDetailIssuePageState createState() =>
      _RepositoryDetailIssuePageState(userName, reposName);
}

// ignore: mixin_inherits_from_not_object
class _RepositoryDetailIssuePageState
    extends BaseListState<RepositoryDetailIssuePage> {
  final String userName;

  final String reposName;

  String searchText;
  String issueState;
  int selectIndex;

  _RepositoryDetailIssuePageState(this.userName, this.reposName);

  _renderEventItem(index) {
    IssueItemViewModel issueItemViewModel =
        IssueItemViewModel.fromMap(pullLoadWidgetControl.dataList[index]);
    return new IssueItemWidget(
      issueItemViewModel,
      onPressed: () {
        NavigatorUtils.goIssueDetail(
            context, userName, reposName, issueItemViewModel.number);
      },
    );
  }

  _resolveSelectIndex() {
    clearData();
    switch (selectIndex) {
      case 0:
        issueState = null;
        break;
      case 1:
        issueState = 'open';
        break;
      case 2:
        issueState = "closed";
        break;
    }
    showRefreshLoading();
  }

  _getDataLogic(String searchString) async {
    if (searchString == null || searchString.trim().length == 0) {
      return await IssueDao.getRepositoryIssueDao(
          userName, reposName, issueState,
          page: pageIndex, needDb: pageIndex <= 1);
    }
    return await IssueDao.searchRepositoryIssue(
        searchString, userName, reposName, this.issueState,
        page: this.pageIndex);
  }

  _createIssue() {
    String title = "";
    String content = "";
    CommonUtils.showEditDialog(
        context: context,
        dialogTitle: "编译Issue",
        onTitleChanged: (titleValue) {
          title = titleValue;
        },
        onContentChanged: (contentValue) {
          content = contentValue;
        },
        onPressed: () {
          if (title == null || title.trim().length == 0) {
            Fluttertoast.showToast(msg: "Issue的标题不能为空");
            return;
          }
          if (content == null || content.trim().length == 0) {
            Fluttertoast.showToast(msg: "Issue的描述不能为空");
            return;
          }
          CommonUtils.showLoadingDialog(context);
          //提交修改
          IssueDao.createIssueDao(
                  userName, reposName, {"title": title, "body": content})
              .then((result) {
            showRefreshLoading();
            Navigator.pop(context);
            Navigator.pop(context);
          });
        },
        needTitle: true,
        titleController: new TextEditingController(),
        valueController: new TextEditingController());
  }

  @override
  bool get wantKeepAlive => true;

  @override
  bool get needHeader => false;

  @override
  bool get autoRefresh => true;

  @override
  requestLoadMore() async {
    return await _getDataLogic(this.searchText);
  }

  @override
  requestRefresh() async {
    return await _getDataLogic(this.searchText);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context); // See AutomaticKeepAliveClientMixin.
    return new Scaffold(
      floatingActionButton: new FloatingActionButton(
          child: new Icon(
            MyIcons.ISSUE_ITEM_ADD,
            size: 55.0,
            color: Color(MyColors.textWhite),
          ),
          onPressed: () {
            _createIssue();
          }),
      backgroundColor: Color(MyColors.mainBackgroundColor),
      appBar: new AppBar(
        leading: new Container(),
        flexibleSpace: SearchInputWidget((value) {
          this.searchText = value;
        }, (value) {
          _resolveSelectIndex();
        }, () {
          _resolveSelectIndex();
        }),
        elevation: 0.0,
        backgroundColor: Color(MyColors.mainBackgroundColor),
        bottom: new SelectItemWidget(["ALL", "OPEN", "CLOSED"], (selectIndex) {
          this.selectIndex = selectIndex;
          _resolveSelectIndex();
        }),
      ),
      body: PullLoadWidget(
        control: pullLoadWidgetControl,
        itemBuilder: (BuildContext context, int index) =>
            _renderEventItem(index),
        refreshCallback: onRefresh,
        loadMoreCallback: onLoadMore,
        refreshKey: refreshIndicatorKey,
      ),
    );
  }
}
